#include "information.h"
#include "ui_information.h"
#include <QTimer>
#include "linechart.h"

Information::Information(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Information)
{
    ui->setupUi(this);
    counter = 0;
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Information::updateCounter);
}

Information::~Information()
{
    delete ui;
}

QPushButton *Information::getButton()
{
    return ui->closeButton;
}


void Information::on_closeButton_clicked()
{
    if(ui->closeButton->text() == "关闭设备" || ui->closeButton->text() == "初始设备"){
        timer->stop();
        ui->label->setPixmap(QPixmap(":/img/chargingWaitting.png"));
        ui->labelText->setText("<font color='black'>空闲中</font>");
        ui->labelState->setPixmap(QPixmap(":/img/stateWaitting.png"));
        ui->labelTime->clear();
        ui->closeButton->setText("打开设备");
    }
    else if(ui->closeButton->text() == "打开设备"){
        ui->label->setPixmap(QPixmap(":/img/chargingFinish.png"));
        ui->labelText->setText("<font color='green'>充电中</font>");
        ui->labelState->setPixmap(QPixmap(":/img/stateCharging.png"));
        counter = 0;
        timer->start(1000);
        ui->closeButton->setText("关闭设备");
    }
}

void Information::updateCounter()
{
    counter++;
    int seconds = counter % 60;
    int minutes = (counter / 60) % 60;
    int hours = counter / 3600;

    QString timeString = QString("<font color='green' size = '5'>充电中 %1:%2:%3</font>")
                             .arg(hours, 2, 10, QChar('0'))
                             .arg(minutes, 2, 10, QChar('0'))
                             .arg(seconds, 2, 10, QChar('0'));

    ui->labelTime->setText(timeString);
}

void Information::on_SeeDetailButton_clicked()
{
    AppearanceWin *appwindow = new AppearanceWin;
    appwindow->show();
}
